package com.cskaoyan.order.biz.handler;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.commons.util.NumberUtils;
import com.cskaoyan.order.biz.callback.SendEmailCallback;
import com.cskaoyan.order.biz.callback.TransCallback;
import com.cskaoyan.order.biz.context.AbsTransHandlerContext;
import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.constant.OrderConstants;
import com.cskaoyan.order.constant.OrderRetCode;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dto.CartProductDto;
import com.cskaoyan.order.utils.GlobalIdGeneratorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 初始化订单 生成订单
 */

@Slf4j
@Component
public class InitOrderHandler extends AbstractTransHandler {

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    GlobalIdGeneratorUtil globalIdGeneratorUtil;


    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        CreateOrderContext context1 = (CreateOrderContext) context;
        Order order = new Order();
        String order_id;
        String order_item_id;
        order_id = globalIdGeneratorUtil.getNextSeq("order_id", 1);
        //订单表的插入
        order.setOrderId(order_id);
        order.setUserId(context1.getUserId());
        order.setBuyerNick(context1.getBuyerNickName());
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setStatus(OrderConstants.ORDER_STATUS_INIT);
        order.setPayment(context1.getOrderTotal());
        int i = orderMapper.insertSelective(order);
        if (i == 0) {
            throw new BizException(OrderRetCode.INIT_ORDER_EXCEPTION.getCode(), OrderRetCode.INIT_ORDER_EXCEPTION.getMessage());
        }

        // 下面开始订单相关商品表的插入
        List<Long> longs = new ArrayList<Long>();
        List<CartProductDto> cartProductDtoList = context1.getCartProductDtoList();
        for (CartProductDto cartProductDto : cartProductDtoList) {
            order_item_id=globalIdGeneratorUtil.getNextSeq("order_item_id", 1);
            OrderItem orderItem = new OrderItem();
            orderItem.setId(order_item_id);
            orderItem.setItemId(cartProductDto.getProductId());
            orderItem.setNum(cartProductDto.getProductNum().intValue());
            orderItem.setOrderId(order_id);
            orderItem.setPicPath(cartProductDto.getProductImg());
            orderItem.setPrice(cartProductDto.getSalePrice().doubleValue());
            BigDecimal totalFee = cartProductDto.getSalePrice().multiply(new BigDecimal(cartProductDto.getProductNum()));
            orderItem.setTotalFee(totalFee.doubleValue());
            orderItem.setTitle(cartProductDto.getProductName());
            orderItem.setStatus(1);
            longs.add(cartProductDto.getProductId());
            int insert = orderItemMapper.insert(orderItem);
            if (insert == 0) {
                throw new BizException(OrderRetCode.INIT_ORDER_EXCEPTION.getCode(), OrderRetCode.INIT_ORDER_EXCEPTION.getMessage());
            }
        }
        context1.setOrderId(order_id);
        context1.setBuyProductIds(longs);
        return true;
    }

}
